package com.guideir.ota;

import android.os.Build;
import android.os.SystemClock;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class UpdateInfo {

    private static final String TAG = "UpdateInfo";
    private static String update_info_json = null;
    public static String otaServerAddr = "http://192.168.137.1:3000";

    public static class OtaPkgInfo {
        private final String url;
        private final String updateMessage;
        private final String versionName;
        private final String md5;

        public String getUrl() { return url; }
        public String getUpdateMessage() { return updateMessage; }
        public String getVersionName() { return versionName; }
        public String getMd5() { return md5; }
        public OtaPkgInfo (String u, String msg, String ver, String m5) {
            url = u;
            updateMessage = msg;
            versionName = ver;
            md5 = m5;
        }
    }


    public static ArrayList<OtaPkgInfo> getOtaPkgInfoList() throws JSONException {
        ArrayList<OtaPkgInfo> result = null;
        boolean finished = false;
        ArrayList<OtaPkgInfo> OtaPkgInfoList = new ArrayList<>();
        if (update_info_json == null) {
            getJson();
            if (getJson() == null) {
                Log.w(TAG, "version_info's Json not found!");
                finished = true;
            }
        }
        if (!finished) {
            JSONArray jsonArray = new JSONArray(update_info_json);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                OtaPkgInfo otaPkgInfo = new OtaPkgInfo(
                        jsonObject.getString("url"),
                        jsonObject.getString("updateMessage"),
                        jsonObject.getString("versionName"),
                        jsonObject.getString("md5"));
                OtaPkgInfoList.add(otaPkgInfo);
            }
            result = OtaPkgInfoList;
        }
        return result;
    }

    public static String getJson() {
        String result;
        Thread threadGetHtml = new Thread(() -> {
            try {
                //String resp = HtmlService.getHtml(otaServerAddr + "/update_info?device=" + Build.DEVICE);
                String resp = HtmlService.getHtml(otaServerAddr + "/list/" + Build.DEVICE + ".js");
                Log.i(TAG, "update_info:" + resp);
                update_info_json = resp;
            } catch (Exception e) {
                Log.w(TAG, "update_info not found!");
                e.printStackTrace();
            }
        });
        threadGetHtml.start();
        SystemClock.sleep(500);
        if (Thread.State.TERMINATED == threadGetHtml.getState()) {
            Log.i(TAG, "Get new version_info");
            result = update_info_json;
        } else {
            Log.w(TAG, "New version_info not found!");
            result = null;
        }
        return result;
    }

}


